﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 注册登录
{
    public partial class ViewWindow : Form
    {
        ManageWindow manageWindow;
        DataSet data;
        public ViewWindow(ManageWindow manageWindow,DataSet data)
        {
            InitializeComponent();
            this.manageWindow = manageWindow;
            this.data = data;
        }

        private void ViewWindow_Load(object sender, EventArgs e)
        {
            listView1.Columns.Add("用户名");
            listView1.Columns.Add("真实姓名");
            listView1.Columns.Add("生日");
            listView1.Columns.Add("邮箱");
            DataTable dataTable = data.Tables["userInfo"];
            for(int i = 0; i < dataTable.Rows.Count; i++)
            {
                ListViewItem item = new ListViewItem();
                item.Text = dataTable.Rows[i][0].ToString();
                item.SubItems.Add(dataTable.Rows[i][2].ToString());
                item.SubItems.Add(dataTable.Rows[i][3].ToString());
                item.SubItems.Add(dataTable.Rows[i][4].ToString());
                listView1.Items.Add(item);
            }
            listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
        }

        private void DelButton_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count < 1) {
                MessageBox.Show("请你先选择要删除的列");
                return;
            }
            string sql = "delete from users where username=@user";
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@user",listView1.SelectedItems[0].Text)
            };
            if (Connection.SqlNoQuery(sql, parameters))
            {
                MessageBox.Show("删除成功");
                // 把listview里面的内容也删掉
                listView1.Items.Remove(listView1.SelectedItems[0]);
            }
            else
            {
                MessageBox.Show("未知错误");
            }
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            String nickName = nameTextBox.Text;
            String psw = pswTextBox.Text;
            String realName = realNameTextBox.Text;
            String birth = birthDateTimePicker.Text;
            String email = emailTextBox.Text;

            if(nickName.Length<1 || psw.Length < 1)
            {
                MessageBox.Show("必要的参数未输入");
                return;
            }

            string sql = "insert into users values(@username,@userpsw,@cn,@birth,@email)";
            SqlParameter[] parameters = new SqlParameter[]
            {
                    new SqlParameter("@username",nickName),
                    new SqlParameter("@userpsw",psw),
                    new SqlParameter("@cn",realName),
                    new SqlParameter("@birth",birth),
                    new SqlParameter("@email",email)
            };
            if (Connection.SqlNoQuery(sql, parameters))
            {
                MessageBox.Show("添加成功");
            }
            else
            {
                MessageBox.Show("SQL执行失败");
                return;
            }
            DataTable dataTable = data.Tables["userInfo"];
            ListViewItem item = new ListViewItem();
            item.Text = nickName;
            item.SubItems.Add(realName);
            item.SubItems.Add(birth);
            item.SubItems.Add(email);
            listView1.Items.Add(item);
        }
    }
}
